package club.itstudio.IContainer.controller.admin;

import club.itstudio.IContainer.annotation.AdminRequired;
import club.itstudio.IContainer.annotation.LoginRequired;
import club.itstudio.IContainer.entity.User;
import club.itstudio.IContainer.exception.BaseException;
import club.itstudio.IContainer.exception.ParamInvalidException;
import club.itstudio.IContainer.service.UserService;
import club.itstudio.IContainer.util.Hash;
import club.itstudio.IContainer.util.Result;
import club.itstudio.IContainer.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @ClassName UserManager
 * @Author alex
 * Date 2021/4/26 4:03 下午
 * Version 1.0
 **/
@RestController
@RequestMapping("/admin/user/manager")
public class UserManagerController {

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    /**
     * 获取用户列表
     */
    @LoginRequired
    @AdminRequired
    @GetMapping
    public Result getUsersInfoByUserType(@RequestParam int page, @RequestParam int limit, @RequestParam int userType) {
        HashMap<String, Object> result = new HashMap<>();
        result.put("total", userService.getUserNumByUserType(userType));
        result.put("content", userService.getUserListByUserType(page, limit, userType));
        return ResultUtil.success(result);
    }

    /**
     * 创建教师
     */
    @LoginRequired
    @AdminRequired
    @PostMapping("/teacher")
    public Result addNewTeacher(@RequestParam String username, @RequestParam String password) {
        if (username == null || username.isEmpty()) throw new ParamInvalidException();
        if (username.length() < 5 || username.length() > 30) throw new ParamInvalidException();
        if (password == null || password.isEmpty()) throw new ParamInvalidException();
        if (password.length() < 6 || password.length() > 30) throw new ParamInvalidException();

        User user = userService.getByUserName(username);
        if (user != null) {
            throw new BaseException("用户已存在");
        }

        User newUser = new User();
        newUser.userName = username;
        newUser.password = Hash.SHA256(password);
        newUser.userType = 1;
        newUser.createdTime = System.currentTimeMillis();
        boolean success = userService.insert(newUser);
        if (success) {
            return ResultUtil.success();
        }else {
            return ResultUtil.error("注册失败，请稍后再试");
        }
    }


    @LoginRequired
    @AdminRequired
    @PostMapping("/reset")
    public Result resetPassword(@RequestParam String userName) {
        User user = userService.getByUserName(userName);
        if (user == null) throw new ParamInvalidException();

        String defaultPassword = Hash.SHA256("12345678");
        user.password = defaultPassword;
        userService.updatePassword(user);

        return ResultUtil.success("密码重置为：12345678");
    }
}
